
const db = wx.cloud.database()
const collectionName = 'active'
export default {
  getAllActive: function({pageNum,pageSize, content}){
    return new Promise((resolve, reject) => {
      
        var skip = (pageNum - 1) * pageSize
        var matchObj = {
      
        }
        if(content){
          matchObj.content = db.RegExp({
            regexp: content,
            options: 'im',
          })
        }
        db.collection('active').aggregate()
        .match(matchObj)
        .sort({
            createDate: -1
        })
        .skip(skip)
        
        .limit(pageSize)
        .end()
        .then(res => {
          resolve(res.list)
        })
    })
  },
  addActive:function(data){
    return new Promise((resolve,reject) => {
      if(data.longitude){
        data.location = new db.Geo.Point(data.longitude, data.latitude)
      }

      data.createDate = new Date()

      db.collection(collectionName).add({
        data: data
      })
      .then(res => {
        console.log(res)
        resolve(res._id)
      })
      .catch(console.error)
    })
  },

  updateActive: async function(data){
    var id = data.id
    delete data.id
    delete data.openid

    if(data.longitude){
      data.location = new db.Geo.Point(data.longitude, data.latitude)
    }

    data.createDate = new Date()

    var res = await db.collection(collectionName).doc(id).update({
    
      data: data
    })
    return res
  },

  deleteActive: async function(id){
    var res = await db.collection(collectionName).doc(id).remove()
    return res
  },
  
  getActive:function(_id){
    return new Promise((resovle,reject) => {
      wx.cloud.callFunction({
        name: 'activeApi',
        data: {
          methodName: 'getActive',
          activeId: _id
        }
      }).then(res => {
        resovle(res.result.list)
      })

    })
  },
  joinActive:function(data){
    return new Promise((resolve,reject) => {
      db.collection('join').add({
        data: {
          openid: data.openid,
          avatarUrl : data.avatarUrl,
          nickName : data.nickName,
          activeId: data.activeId,
          createDate: new Date()
        }
      }).then(res => {
        resolve(res._id)
      })
    })
  },

  deleteJoin: async function(activeId){
    var res = await db.collection('join').where({
      activeId: activeId
    }).remove()

    return res
  },

  getMyActive:function(param){
    return new Promise((resolve,reject) => {
      param.methodName = 'getMyActive'
      wx.cloud.callFunction({
        name: 'activeApi',
        data: param
   
      }).then(res => {
        resolve(res.result.list)
      })
    })
  },
  getMyJoinActive:function(param){
    
    return new Promise((resolve,reject) => {
      wx.cloud.callFunction({
        name: 'activeApi',
        data: {
          methodName: 'getMyJoinActive',
          pageNum : param.pageNum,
          pageSize : param.pageSize
        }
    
      }).then(res => {
        resolve(res.result.list)
      })
    })
  },
  getIsShow:function(){
    return new Promise((resolve, reject) => {
      db.collection('show') .get().then(res => {
        
        resolve(res.data)
        
      })
    })
  }
}